package com.metagurukul.informer.servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.metagurukul.informer.service.ShowTagsService;
import com.metagurukul.informer.valueObjects.TagQuestionCounter;

/**
 * Servlet implementation class ShowTags
 */
@WebServlet("/ShowTags")
public class ShowTags extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowTags() {
        super();
        
    }

    
    /**
     * servlet to interact with dao layer and get all tags from DAO
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void doPerform(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	
    	
		try {
			RequestDispatcher rd=null;

			ShowTagsService service=new ShowTagsService();
			List<TagQuestionCounter> list=service.getTags();
			if(list!=null){
				
				HttpSession session=request.getSession();
				session.setAttribute("listOfTags",list );
				rd=request.getRequestDispatcher("/showTags.jsp");
				
			} else {
				
				rd=request.getRequestDispatcher("/Error.jsp");
			}

			rd.forward(request,response);
		} catch (SQLException e) {
			response.sendRedirect("Error.jsp");
		}
    	
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
		doPerform(request , response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPerform(request , response);
	}

}
